/**
 * @param {number[]} nums
 * @return {number}
 * 
 * 运行时间: 100 ms
 * 内存消耗: 40.1 MB
 */
var pivotIndex = function(nums) {
  if (!nums) return -1;
  if (!(Array.isArray(nums) && nums.length)) return -1;
  if (nums.length === 1) return 0;
  if (nums.length === 2) return -1;
  if (nums.length === 3) {
    if (nums[0] === nums[2]) return 1;
    else return -1;
  }
  let sum = 0
  for (let i = 0; i < nums.length; i++) {
    sum += nums[i]
  }

  let leftTotal = 0;
  for (let i = 0; i < nums.length; i++) {
    let current = nums[i]
    sum -= current
    if (leftTotal == sum) {
      return i
    }
    leftTotal += current
  }
  return -1
};


// const nums = [0]
// const nums = [1,1]
// const nums = [1,2,1]
const nums = [1,3,1]
// const nums = [1,2,1,2]
// const nums = [1,7,3,6,5,6]

// console.log(nums)
// console.log(pivotIndex(nums))


// 异常情况
console.log(pivotIndex(null))